package com.ky.manage.activity.indoor;

import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.ky.manage.R;
import com.ky.manage.adapter.FragmentTabAdapter;
import com.ky.manage.base.BaseActivity;
import com.ky.manage.fragment.HomeFragment;
import com.ky.manage.utils.StringUtil;
import com.ky.manage.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

/**
 * 户内整改记录
 */
public class IndoorMendListActivity extends BaseActivity {

    private List<Fragment> mFragmentList = new ArrayList<>();
    private HomeFragment allTabFragment, modifiedTabFragment, toModifyTabFragment;

    private ViewPager mPager;
    private RadioGroup mTabRg;
    private EditText searchEt;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_indoor_mend_list;
    }

    @Override
    protected void initView() {
        findView(R.id.back_iv, v -> {
            finish();
        });
        findView(R.id.msg_iv, v -> {
            ToastUtils.showRoundRectToast("点击消息按钮");
        });
        mTabRg = findView(R.id.tab_rg);
        mTabRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.all_tab_rb) {
                    mPager.setCurrentItem(0, true);
                } else if (checkedId == R.id.tab_modified_rb) {
                    mPager.setCurrentItem(1, true);
                } else if (checkedId == R.id.tab_to_modify_rb) {
                    mPager.setCurrentItem(2, true);
                }
            }
        });
        searchEt = findView(R.id.search_et);
        searchEt.setOnEditorActionListener((v, actionId, event) -> {
            switch (actionId) {
                case EditorInfo.IME_ACTION_SEARCH:
                    searchBykeyword(searchEt.getText().toString().trim());
                    break;
                default:
                    break;
            }
            return false;
        });
        findView(R.id.search_et_iv, v -> {
            searchBykeyword(searchEt.getText().toString().trim());
        });
        findView(R.id.msg_iv, v -> {
            ToastUtils.showRoundRectToast(StringUtil.getResString(R.string.to_do_prompt));
        });
        mPager = findView(R.id.view_pager);
        //全部
        allTabFragment = new HomeFragment("");
        //状态（0正常， 1待整改， 2已整改）
        modifiedTabFragment = new HomeFragment("2");
        toModifyTabFragment = new HomeFragment("1");
        mFragmentList.add(allTabFragment);
        mFragmentList.add(modifiedTabFragment);
        mFragmentList.add(toModifyTabFragment);

        mPager.setAdapter(new FragmentTabAdapter(getSupportFragmentManager(), mFragmentList));
        mPager.setOffscreenPageLimit(1);
        mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                RadioButton radioButton = (RadioButton) mTabRg.getChildAt(position);
                radioButton.setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    /**
     * 关键词搜索
     * @param searchStr
     */
    private void searchBykeyword(String searchStr) {
        /*HomeFragment homeFragment = (HomeFragment) mFragmentList.get(mPager.getCurrentItem());
        homeFragment.searchBykeyword(searchStr);
        SCLogFragment scLogFragment = (SCLogFragment) mFragmentList.get(mPager.getCurrentItem());
        scLogFragment.searchBykeyword(searchStr);*/
    }

    @Override
    protected void initData() {

    }
}